package com.jiyun.passenger.controller;

import com.alibaba.fastjson.JSON;
import com.jiyun.passenger.service.OrderInfoService;
import com.jiyun.pojo.OperatePassengerCharges;
import com.jiyun.pojo.order.OrderCost;
import com.jiyun.pojo.order.OrderInformation;
import com.jiyun.pojo.order.OrderTime;
import com.jiyun.pojo.order.vo.*;
import com.jiyun.result.Result;
import io.swagger.v3.core.util.Json;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/order")
@Slf4j
public class OrderInfoController {
    @Autowired
    OrderInfoService orderInfoService;
    @Autowired
    RedisTemplate redisTemplate;

    @PostMapping("/addOrderInfo")
    public Result addOrderInfo(@RequestBody OrderInfoVo orderInfoVo) {
        log.info("开始下单：{}", orderInfoVo);
        return orderInfoService.addOrderInfo(orderInfoVo);
    }

    // 根据orderId查询订单信息
    @PostMapping("/findByOrder")
    public Result findByOrder(@RequestBody OrderInformation orderInformation){
        OrderInformation information = orderInfoService.findByOrder(orderInformation);
        return Result.ok(information);
    }

    // 根据orderId查询下单时间
    @PostMapping("/findByOrderIdTime")
    public Result findByOrderIdTime(@RequestBody OrderTime orderTime){
        OrderTime orderTime1 = orderInfoService.findByOrderIdTime(orderTime);
        return Result.ok(orderTime1);
    }

    @RequestMapping("/findByMileageFee")
    public Result findByMileageFee(){
        OperatePassengerCharges mileageFee2 = orderInfoService.findByMileageFee();
        return Result.ok(mileageFee2);
    }

    @PostMapping("/findByPassenger")
    public Result findByPassenger(@RequestBody OrderInfoVo orderInfoVo){
       try {
           log.info("查询乘客信息：{}", orderInfoVo);
           redisTemplate.opsForValue().set("passenger", JSON.toJSONString(orderInfoVo) ,30, TimeUnit.MINUTES);
           return Result.ok();
       }catch (Exception e){
           return Result.error("查询失败");
       }
    }


}
